package com.yuandian.management.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yuandian.api.management.entity.ConfSegment;
import com.yuandian.bpm.admin.api.entity.SysDictItem;
import com.yuandian.bpm.admin.api.feign.RemoteDictService;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.constant.DictType;
import com.yuandian.management.service.ConfSegmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 网端配置表-Controller
 * @author zfsn
 * @date 2023-3-22
 */
@Api(tags = "网端配置表功能接口")
@RestController
@RequestMapping("/segment")
public class ConfSegmentController {

	@Autowired
	private ConfSegmentService confSegmentService;

	@Autowired
	private RemoteDictService remoteDictService;


	/**
	 * 通过主键查询数据
	 * @param id 主键集合
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "主键查询", notes = "通过主键查询返回码配置，支持批量查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "主键数组", paramType = "path", required = true, dataTypeClass = Integer.class)
	})
	@GetMapping("/queryById/{id}")
	public R<ConfSegment> getSegmentById(@PathVariable("id") Integer id) {
		return R.ok(confSegmentService.getById(id));
	}

	/**
	 * 通过系统主键查询数据
	 * @param systemIds 主键集合
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键查询数据", notes = "通过主键查询数据，支持批量查询")
	@GetMapping("getSegmentsBySystemId/{systemIds}")
	public R<List<ConfSegment>> queryByIds(@PathVariable("systemIds") List<Integer> systemIds) {
		QueryWrapper<ConfSegment> queryWrapper = new QueryWrapper<>();
		queryWrapper.in("system_id", systemIds);
		return R.ok(confSegmentService.list(queryWrapper));
	}



	/**
	 * 获取网端类型名称
	 * @param segmentId 主键集合
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "获取网端类型名称", notes = "获取网端类型名称")
	@GetMapping("getSegmentTypeNameBySegmentId/{segmentId}")
	public R<ConfSegment> getSegmentTypeNameBySegmentId(@PathVariable("segmentId") Integer segmentId) {
		ConfSegment confSegment = confSegmentService.getById(segmentId);
		Map<String, String> segmentTypeAndNameMap = remoteDictService.getDictByType(DictType.SEGMENT_TYPE).getData().stream().collect(Collectors.toMap(SysDictItem::getItemValue, SysDictItem::getLabel));
		confSegment.setTypeName(segmentTypeAndNameMap.get(String.valueOf(confSegment.getType())));
		return R.ok(confSegment);
	}

}